$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'basics/bgoodstype/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden:true},
			{ label: '类型名称', name: 'typeName', index: 'type_name', width: 80 ,align:"center"}, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 ,align:"center"}, 			
			{ label: '创建人', name: 'createUser', index: 'create_user', width: 80 , hidden:true}, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,align:"center", formatter:function(value,options,row){
                return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}}, 			
			{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 ,align:"center", hidden:true, formatter:function(value,options,row){
                return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}}, 			
			{ label: '状态', name: 'isUsed', index: 'is_used', width: 80 ,align:"center", formatter: function(value, options, row){
				return value === 1 ? 
						'<span class="label label-success" value="1">启用</span>' : 
						'<span class="label label-danger" value="0">禁用</span>';
			}}, 			
			{ label: '是否启用,0未启用，1启用', name: 'isUsed', index: 'is_used', width: 80 , hidden:true}, 			
			{ label: '是否删除，1删除，0未删除', name: 'isDel', index: 'is_del', width: 80 , hidden:true}			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "goodsTypeList.list",
            page: "goodsTypeList.pageNum",
            total: "goodsTypeList.pages",
            records: "goodsTypeList.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bGoodsType: {},
        search:{
            typeName:'',
        },
        ruleValidate:{//验证
            typeName: [{ required: true,trigger: 'blur',message:'请输入类型名称！'}],
        },
        isUsed:true,//是否有效 默认有效
        isShow:false,//是否只读
	},
	methods: {
        reset:function(event) {
            vm.search = {
                typeName:''
            }
            vm.reload();
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bGoodsType = {};
            vm.isShow=false;//可读
            vm.bGoodsType.isUsed=1;//默认有效
            vm.isUsed=true;//默认有效
            this.$refs['bGoodsType'].resetFields();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (name) {
          /*  this.$Spin.show({
                render: function h() {
                    return h('div', [
                        h('Icon', {
                            'class': 'demo-spin-icon-load',
                            props: {
                                type: 'ios-loading',
                                size: 18
                            }
                        }),
                        h('div', 'Loading')
                    ])
                }
            });*/
            this.$refs[name].validate(function(valid){
                console.log(valid)
                if (valid) {
                    var url = vm.bGoodsType.id == null ? "basics/bgoodstype/save" : "basics/bgoodstype/update";

                    var param = {
                        "goodsType": vm.bGoodsType
                    };

                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(param),
                        success: function (r) {
                           // vm.$Spin.hide();
                            if (r.code === 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }else{
                   // vm.$Spin.hide();
                }
            });
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "basics/bgoodstype/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            if(vm.title=='详情'){
                vm.isShow=true;
            }else{
                vm.isShow=false;
            }
			$.get(baseURL + "basics/bgoodstype/info/"+id, function(r){
                vm.bGoodsType = r.map.goodsType;
                vm.isUsed=vm.bGoodsType.isUsed==1?true:false;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    typeName: vm.search.typeName,

                }
            }).trigger("reloadGrid");
		},
		detail:function(event){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.showAdd = false;
		    vm.title = "详情";
		        
		    vm.getInfo(id);

		},
        changeUsed:function(){
            vm.bGoodsType.isUsed=vm.isUsed?1:0;
        },
	}
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}